
package com.yimixian.app.rest.api;

import com.yimixian.app.model.CancelOrder;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.model.Metadata;
import com.yimixian.app.model.Order;
import com.yimixian.app.model.SearchKeyResult;

import org.json.JSONArray;
import org.json.JSONObject;

import retrofit.Callback;
import retrofit.client.Response;

import java.util.ArrayList;
import java.util.List;

public class OrdersDetailAPI extends YmxBaseAPI<Order> {

    public static final String ORDER_INFO = "/v5/orders/info";
    private String token = "";
    private int orderId;

    public OrdersDetailAPI(int order_id, String token, Callback<Response> callback) {
        super(callback);
        this.token = token;
        orderId = order_id;
    }

    @Override
    public String getRelativeURL() {
        return ORDER_INFO;
    }

    public void getOrderInfo() {
        mYmxRestService.getOrdersInfo(token, orderId, mCallback);
    }

    @Override
    public Order parseJson(JSONObject object) {
        Order mOrder = new Order();
        if (object != null) {
            mOrder.id = object.optInt("id");
            mOrder.status = object.optInt("status");
            List<Order.PaymentMethods> payMethodList = new ArrayList<>();
            JSONArray payMethodArray = object.optJSONArray("payment_methods");
            if (payMethodArray != null) {
                for (int j = 0; j < payMethodArray.length(); j++) {
                    JSONObject payMethodObject = payMethodArray.optJSONObject(j);
                    if (payMethodObject != null) {
                        Order.PaymentMethods paymentMethods = new Order.PaymentMethods();
                        paymentMethods.setId(payMethodObject.optString("id"));
                        paymentMethods.setName(payMethodObject.optString("name"));
                        payMethodList.add(paymentMethods);
                    }
                }
            }

            mOrder.paymentMethods = payMethodList;

            mOrder.paymentId = object.optString("payment_id");
            mOrder.needsPayment = object.optBoolean("needs_payment");
            mOrder.paymentDueAt = object.optString("payment_due_at");
            mOrder.complainUrl = object.optString("complain_url");
            mOrder.priceToPay = object.optString("price_to_pay");
            mOrder.orderStatus = object.optString("order_status");

            List<Metadata> metadataList = new ArrayList<>();
            JSONArray priceComponents = object.optJSONArray("price_components");
            if (priceComponents != null) {
                for (int j = 0; j < priceComponents.length(); j++) {
                    JSONObject priceComponentsObject = priceComponents.optJSONObject(j);
                    if (priceComponentsObject != null) {
                        Metadata metadata = new Metadata();
                        metadata.name = priceComponentsObject.optString("name");
                        metadata.url = priceComponentsObject.optString("url");
                        metadata.value = priceComponentsObject.optString("value");
                        metadataList.add(metadata);
                    }
                }
            }
            mOrder.priceComponents = metadataList;

            mOrder.successUrl = object.optString("success_url");

            Order.DeliveryInfo deliveryInfo = new Order.DeliveryInfo();
            JSONObject deliveryInfoObject = object.optJSONObject("delivery_info");
            if (deliveryInfoObject != null) {
                deliveryInfo.index = deliveryInfoObject.optInt("index");
                JSONArray statusArray = deliveryInfoObject.optJSONArray("statuses");
                List<String> statuses = new ArrayList<>();
                if (statusArray != null) {
                    for (int j = 0; j < statusArray.length(); j++) {
                        String str = statusArray.optString(j);
                        statuses.add(str);
                    }
                }
                deliveryInfo.statuses = statuses;
            }
            mOrder.deliveryInfo = deliveryInfo;

            mOrder.createdAt = object.optString("created_at");
            mOrder.totalCount = object.optInt("total_count");

            JSONArray metadataArray = object.optJSONArray("metadata");
            List<Metadata> metadata = new ArrayList<>();
            if (metadataArray != null) {
                for (int j = 0; j < metadataArray.length(); j++) {
                    JSONObject metadataObject = metadataArray.optJSONObject(j);
                    if (metadataObject != null) {
                        Metadata met = new Metadata();
                        met.name = metadataObject.optString("name");
                        met.url = metadataObject.optString("url");
                        met.value = metadataObject.optString("value");
                        metadata.add(met);
                    }
                }
            }
            mOrder.metadata = metadata;

            JSONArray itemsArray = object.optJSONArray("items");
            List<GoodsItem> goodsItems = new ArrayList<>();
            if (itemsArray != null) {
                for (int j = 0; j < itemsArray.length(); j++) {
                    JSONObject goodsItemObject = itemsArray.optJSONObject(j);
                    if (goodsItemObject != null) {
                        GoodsItem met = new GoodsItem();
                        met.name = goodsItemObject.optString("name");
                        met.id = goodsItemObject.optInt("id");
                        met.canComment = goodsItemObject.optBoolean("can_comment");
                        met.tasteDesc = goodsItemObject.optString("taste_desc");
                        met.freshnessDesc = goodsItemObject.optString("freshness_desc");
                        met.count = goodsItemObject.optString("count");
                        met.price = goodsItemObject.optString("price");
                        met.itemAttr = goodsItemObject.optString("item_attr");
                        met.textIndent = goodsItemObject.optString("text_indent");
                        goodsItems.add(met);
                    }
                }
            }
            List<Order.orderStatus> statusItems = new ArrayList<>();

            JSONObject statusObject = object.optJSONObject("detail_delivery_info");
            if (statusObject != null) {
                JSONArray itemsStatusArray = statusObject.optJSONArray("statuses");
                if (itemsStatusArray != null && itemsStatusArray.length() > 0) {
                    for (int j = 0; j < itemsStatusArray.length(); j++) {
                        JSONObject ItemObject = itemsStatusArray.optJSONObject(j);
                        if (ItemObject != null) {
                            Order.orderStatus met = new Order.orderStatus();
                            met.name = ItemObject.optString("name");
                            met.desc = ItemObject.optString("desc");
                            met.date = ItemObject.optString("date");
                            met.url = ItemObject.optString("url");
                            met.active = ItemObject.optBoolean("active");
                            statusItems.add(met);
                        }
                    }
                }
            }

            JSONObject shareObject = object.optJSONObject("share");

            mOrder.statusItems = statusItems;
            mOrder.items = goodsItems;
            mOrder.indexPage = object.optInt("tab_index");
            mOrder.statusUrl = object.optString("status_url");
            mOrder.commentBtnName = object.optString("comment_btn_name");
            mOrder.complainBtnName = object.optString("complain_btn_name");
            mOrder.commentUrl = object.optString("comment_url");
            if (shareObject != null) {
                mOrder.shareImgUrl = shareObject.optString("share_img_btn");
                JSONObject shareDataObject = shareObject.optJSONObject("share_data");
                mOrder.shareUrl = shareDataObject.optString("shareurl");
                mOrder.shareIcon = shareDataObject.optString("icon_imgurl");
                mOrder.shareContent = shareDataObject.optString("share_content");
                mOrder.shareTitle = shareDataObject.optString("title");

            }

            JSONObject cancelObject = object.optJSONObject("cancel_order");
            CancelOrder cancelOrder = new CancelOrder();
            if(cancelObject != null){
                cancelOrder.cancel_button_visible = cancelObject.optBoolean("cancel_button_visible");
                cancelOrder.cancel_button_message = cancelObject.optString("cancel_button_message");
                cancelOrder.cancel_button_title = cancelObject.optString("cancel_button_title");
            }
            mOrder.cancelOrder = cancelOrder;

        }
        return mOrder;
    }

}
